package cn.ren.cloud.mq.consumer;

import cn.ren.cloud.domain.MediaFile;
import cn.ren.cloud.service.IMediaFileService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author 23508
 */
@Component
@RocketMQMessageListener(consumerGroup = "service-media-producer",
        topic = "media-topic",
        selectorExpression = "push:tag",
        messageModel = MessageModel.CLUSTERING)
public class MediaPushConsumer implements RocketMQListener<MessageExt> {

    private final IMediaFileService mediaFileService;

    public MediaPushConsumer(IMediaFileService mediaFileService) {
        this.mediaFileService = mediaFileService;
    }

    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String str = new String(body, StandardCharsets.UTF_8);
        MediaFile mediaFile = JSON.parseObject(str, MediaFile.class);
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}
